<template>
    <el-dialog @close="cancleEditProcess" v-model="dialogVisible" title="编辑工序信息" destroy-on-close center>
        <template #default>
            <el-form :model="process" label-width="100px" label-position="left">
                <el-form-item label="工序编码">
                    <el-input v-model="process.processCode" aria-placeholder="工序编码" />
                </el-form-item>
                <el-form-item label="工序名称">
                    <el-input v-model="process.processName" aria-placeholder="工序名称" />
                </el-form-item>
                <el-form-item label="是否启用">
                    <el-radio-group v-model="process.processStatus">
                        <el-radio :label="1">是</el-radio>
                        <el-radio :label="2">否</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="工序说明">
                    <el-input type="textarea" v-model="process.description" aria-placeholder="工序说明" />
                </el-form-item>
                <el-form-item label="备注">
                    <el-input type="textarea" v-model="process.notes" aria-placeholder="备注" />
                </el-form-item>
            </el-form>
            <StepItem :processId="processId"/>
        </template>
        <!-- footer插槽中放两个按钮 -->
        <template #footer>
            <el-row>
                <el-col :span="12">
                    <el-button type="primary" @click="editProcess">确定</el-button>
                </el-col>
                <el-col :span="12">
                    <el-button type="primary" @click="cancleEditProcess">取消</el-button>
                </el-col>
            </el-row>
        </template>
    </el-dialog>
</template>
<script>
import { watch } from "vue";
import ProcessRequest from '../../../api/process.js';
import StepItem from "@/views/pro/process/step/step-item.vue";
export default {
    components: {StepItem},
    props: ["visible","processId"],
    emits: ["optionCompleted"], // 对外暴露一个事件，用来传递编辑工序的操作结果
    data() {
        return {
            process: {},// 绑定要编辑的工序信息
            dialogVisible: false,// 编辑工序窗口的显示与隐藏
        }
    },
    methods: {
        /**
         * 取消编辑工序的事件
         */
        cancleEditProcess(){
            // 通知父组件  操作完成，让父组件关闭窗体
            this.$emit("optionCompleted");
        },
        /**
         * 编辑工序信息
         */
        editProcess(){
            ProcessRequest.updateProcess(this.process,res=>{
                if (res.code == '200'){
                    this.$message({
                        type: 'success',
                        message: '编辑工序成功!'
                    });
                    // 通知父组件  操作完成，让父组件关闭窗体
                    this.$emit("optionCompleted");
                }else if(res.code == '300'){
                    this.$message({
                        type: 'warning',
                        message: '工序' + (res.msg.indexOf("processName") > 0 ?"名称":"类型") + "重复"
                    });
                }else{
                    this.$message({
                        type: 'error',
                        message: '编辑工序失败!'
                    });
                }
            });
        },

        /**
         * 加载工序信息
         */
        loadProcess(){
            ProcessRequest.getById(this.processId,res=>{
                console.log(res)
                if(res.data != null)
                    this.process = res.data;
            });
        }
    }, mounted(props) {
        this.loadProcess()
        // 侦听props   一旦发生变化，就会出发下面的函数
        watch(() => this.visible, (newVal, oldVal) => {
            // 将最新的值设置给弹窗的dialogVisible属性
            this.dialogVisible = newVal;
        });

        watch(() => this.processId, (newVal, oldVal) => {
            // 如果processId 发生变化，重新加载字典信息
            this.loadProcess();
        });
    }
}
</script>